package com.wits.qso.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.wits.platform.dao.impl.BaseDaoImpl;
import com.wits.qso.dao.custom.MyRegisterCourseDaoCustom;
import com.wits.qso.entity.MyRegisterCourse;

/**
 * 我注册的课程数据DAO实现
 */
@Component
public class MyRegisterCourseDaoImpl extends BaseDaoImpl<MyRegisterCourse, String>
		implements MyRegisterCourseDaoCustom {
	@SuppressWarnings("unchecked")
	public List<MyRegisterCourse> findMyRegisterCourses(Map<String, Object> param) {
		if (param == null) {
			return new ArrayList<MyRegisterCourse>();
		}
		StringBuffer buf = new StringBuffer(
				" select qcr.cr_id ,qcr.stu_id,stu.stu_name ,qcr.ter_id,ter.ter_name,qcr.course_id ,course.course_title,qcr.cr_status ,qcr.cr_message ,qcr.date_apply ,qcr.date_auth,qcr.remark,qcr.user_cre,qcr.date_cre,qcr.user_upd,qcr.date_upd  from qso_course_register qcr left join qso_student stu on qcr.stu_id=stu.stu_id left join qso_teacher ter on qcr.ter_id=ter.ter_id left join qso_course course on qcr.course_id=course.course_id  where 1=1  ");
		if (param.get("cr_id") != null) {
			buf.append(" and qcr.cr_id=:cr_id ");
		}
		if (param.get("ter_id") != null) {
			buf.append(" and qcr.ter_id=:ter_id ");
		}
		if (param.get("ter_name") != null) {
			buf.append(" and ter.ter_name like %:ter_name% ");
		}
		if (param.get("stu_id") != null) {
			buf.append(" and qcr.stu_id =:stu_id ");
		}
		if (param.get("course_id") != null) {
			buf.append(" and qcr.course_id =:course_id ");
		}
		if (param.get("course_title") != null) {
			buf.append(" and course.course_title like %:course_title% ");
		}
		if (param.get("cr_status") != null) {
			buf.append(" and qcr.cr_status =:cr_status ");
		}
		if (param.get("cr_message") != null) {
			buf.append(" and qcr.cr_message like %:cr_message% ");
		}
		if (param.get("date_apply") != null) {
			buf.append(" and qcr.date_apply =:date_apply ");
		}
		if (param.get("date_auth") != null) {
			buf.append(" and qcr.date_auth =:date_auth ");
		}

		Query a = em.createNativeQuery(buf.toString(), MyRegisterCourse.class);
		if (param.get("cr_id") != null) {
			a.setParameter("cr_id", param.get("cr_id"));
		}
		if (param.get("ter_id") != null) {
			a.setParameter("ter_id", param.get("ter_id"));
		}
		if (param.get("ter_name") != null) {
			a.setParameter("ter_name", param.get("ter_name"));
		}
		if (param.get("stu_id") != null) {
			a.setParameter("stu_id", param.get("stu_id"));
		}
		if (param.get("course_id") != null) {
			a.setParameter("course_id", param.get("course_id"));
		}
		if (param.get("course_title") != null) {
			a.setParameter("course_title", param.get("course_title"));
		}
		if (param.get("cr_status") != null) {
			a.setParameter("cr_status", param.get("cr_status"));
		}
		if (param.get("cr_message") != null) {
			a.setParameter("cr_status", param.get("cr_message"));
		}
		if (param.get("date_apply") != null) {
			a.setParameter("date_apply", param.get("date_apply"));
		}
		if (param.get("date_auth") != null) {
			a.setParameter("date_auth", param.get("date_auth"));
		}
		@SuppressWarnings("rawtypes")
		List list = a.getResultList();
		return list;
	}
}
